import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const RouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
    return RouterPush.call(this, to).catch(err => err)
}

const RouterReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function replace(to) {
    return RouterReplace.call(this, to).catch(err => err)
}

// 路由模块
import dashboardRoute from './modules/dashboard'
import systemRoute from './modules/system'
import addressRoute from './modules/address'
import userRoute from './modules/user'
import orderRoute from './modules/order'
import configRoute from './modules/config'
import expressCardRoute from './modules/expressCard'
import workOrderRoute from './modules/workOrder'
import financeRoute from './modules/finance'
import fundRoute from './modules/fund'

// 本地权限路由
const constantRoutes = [systemRoute, addressRoute, userRoute, orderRoute, configRoute, expressCardRoute, workOrderRoute, financeRoute, fundRoute]

export { dashboardRoute, constantRoutes }

// 本地固定路由
const baseRouters = [
    dashboardRoute,
    {
        path: '/',
        name: 'Home',
        component: () => import('@/views/home/index.vue'),
        meta: {
            title: '首页'
        },
        hidden: true
    },
    {
        path: '/login',
        component: () => import('@/views/login/index.vue'),
        name: 'Login',
        meta: {
            title: '登录'
        },
        hidden: true
    },
    {
        path: '/404',
        name: '404',
        component: () => import('@/views/404'),
        hidden: true
    }
]

const createRouter = () =>
    new VueRouter({
        mode: 'history',
        base: process.env.BASE_URL,
        routes: baseRouters
    })

const router = createRouter()

// 重置路由
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher
}

export default router
